require "open-uri"
require "prawn"
pdf.font "Helvetica"


pdf = Prawn::Document.new(:page_layout => :portrait)
logo = "#{RAILS_ROOT}/public/images/LogoFactura.jpg"
pdf.image logo, :at => [0,750], :width => 300

pdf.stroke do
    pdf.rectangle [0,650], 550, 670
end


pdf.indent(50,50) do
    pdf.text "\n\n\n" + @titulofactura, :align => :center, :size => 15, :style => :bold
end


pdf.move_down 20
pdf.text "#{@compania.nombre}",
      :size => 20,
      :align => :center,
      :style => :bold
pdf.text  "#{@compania.rif}",
      :size => 14,
      :align => :center,
      :style => :bold
pdf.move_down 10
pdf.text "Direccion: #{@compania.direccion}", :size => 10, :align => :center
pdf.text "Telefono: #{@compania.telefono}", :size => 10, :align => :center
pdf.move_down 20

pdf.indent(30) do
    data = [["Fecha:", "#{@orden_servicio.fecha_orden.strftime("%d-%m-%Y")}"]]

    pdf.table(data,:cell_style => { :borders => [] }) do |tablaFactura|
        tablaFactura.rows(1..1)
    end
    pdf.text "\n\n"
end

pdf.indent(50) do

    pdf.text "#{@cliente.nombre} #{@cliente.apellido}", :size => 20, :align => :left, :style => :bold
    pdf.text "#{@cliente.correo}", :size => 14, :align => :left

end

pdf.move_down 10
pdf.indent(50) do
    data = [["Factura#:" ,"000"+"#{@orden_servicio.id}"],
            ["Receptor", "#{@orden_servicio.nombre_receptor} #{@orden_servicio.apellido_receptor}"],
            ["Direccion Envio:", "Urb:"+"#{@orden_servicio.residencia_calle}," ,"Casa/Apto:","#{@orden_servicio.apartamento_num_casa}, " + "#{@orden_servicio.urbanizacion},"+"#{@orden_servicio.ciudad},"+"#{@orden_servicio.pais}"],
            ["Direccion Recoleccion","#{@orden_servicio.nombre_receptor}"]]


    pdf.table(data,:cell_style => { :borders => [] }) do |tablaFactura|
        tablaFactura.rows(1..4)
    end
    
end


pdf.move_down 20

pdf.indent(50) do

    top = [["Tipo", "Descripcion" ,"Peso (g)"]]
    items = @paquete.map do |paquete|
      [
        paquete.tipo,
        paquete.descripcion,
        paquete.peso,
        ""
      ]
    end
    pdf.table top, :row_colors => ["d2e3ed"], :column_widths => { 0 => 80, 1 => 200, 2 => 60, 3 => 80 }
    pdf.table items, :row_colors => ["FFFFFF"], :column_widths => { 0 => 80, 1 => 200, 2 => 60, 3 => 80}
    fin = [["","","","Subtotal","#{@subtotal.round(2)}"],
           ["","","","IVA","#{@orden_servicio.iva.round(2)}"],
           ["","","","Total","#{@orden_servicio.total.round(2)}"+"  BsF."]]
    
    pdf.indent(310) do
        pdf.table(fin,:cell_style => { :borders => [] }) do |tablatotal|
            tablatotal.rows(1..3)
        end
    end
end


pdf.move_down 25
pdf.image open("#{@qr}"), :at => [420,120], :width => 125